package com.shanling.shanlingcontroller.view;

import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.support.annotation.NonNull;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.TextView;


import com.shanling.shanlingcontroller.R;
import com.shanling.shanlingcontroller.adapter.CommonAdapter;
import com.shanling.shanlingcontroller.adapter.MyblutoothItemAdapter;
import com.shanling.shanlingcontroller.adapter.ViewHolder;
import com.shanling.shanlingcontroller.bean.LanguageBean;
import com.shanling.shanlingcontroller.bean.MybluetoothDevice;
import com.shanling.shanlingcontroller.utils.DpUtils;
import com.shanling.shanlingcontroller.utils.PreferenceUtil;

import org.litepal.LitePal;

import java.util.List;

/**
 * Created by Admin on 2018/3/29.
 */

public class TopMiddlePopup extends PopupWindow implements AdapterView.OnItemClickListener {

    private Context context;
    private MyblutoothItemAdapter adapter;
    private ListView listView;
    private OnItemClickListener listener;
    private LayoutInflater inflater;

    public TopMiddlePopup(Context context, @NonNull List<MybluetoothDevice> mData){
        this.context = context;
        inflater = LayoutInflater.from(context);
        adapter=new MyblutoothItemAdapter(mData,context);
        View popupView = inflater.inflate(R.layout.layout_popup_window, null);
        listView = (ListView) popupView.findViewById(R.id.list_view);
        listView.setOnItemClickListener(this);
        listView.setAdapter(adapter);
        this.setContentView(popupView);
    }
    private void initPopupWindow(){
        this.setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
        this.setHeight(DpUtils.dpToPx(context, adapter.getCount() * 55));
        this.setBackgroundDrawable(new ColorDrawable(context.getResources().getColor(R.color.bg)));
        this.setFocusable(true);
        this.setTouchable(true);
        this.setOutsideTouchable(true);
        this.update();
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        if (listener != null){
            listener.onItemClick(view, position);
            this.dismiss();
        }
    }

    public void show(View view){
        if (view != null){
            List<MybluetoothDevice> mybluetoothDevices = LitePal.findAll(MybluetoothDevice.class);
            adapter.refresh(mybluetoothDevices);
            initPopupWindow();
            this.showAsDropDown(view);
        }
    }

    public interface OnItemClickListener {
        void onItemClick(View v, final int position);
    }

    public void setOnItemClickListener(OnItemClickListener listener) {
        this.listener = listener;
    }
}
